<script setup lang="ts">
import type { FormInstance, FormRules } from "element-plus";
import { RouterLink } from "vue-router";
import { reactive, ref } from "vue";
import axios from "axios";
import { performCheck } from "@/assets/script/invisible";
import Github from "@/components/icons/github.vue";
import Gitee from "@/components/icons/gitee.vue";
import OLink from "@/components/oauth/olink.vue";

const element = ref<FormInstance>();
const loading = ref<boolean>(false);
const error = ref<string>("");
const form = reactive({
  username: "",
  password: "",
  captcha: "",
});

const rules = reactive<FormRules>({
  username: [
    { required: true, message: 'Please input username', trigger: 'blur' },
    { min: 3, max: 14, message: 'Length should be 3 to 14', trigger: 'change' },
  ],
  password: [
    { required: true, message: 'Please input password', trigger: 'blur' },
    { min: 6, max: 26, message: 'Length should be 6 to 26', trigger: 'change' },
  ],
  captcha: [
    { required: true, message: '', trigger: 'blur' },
  ],
})

async function submit(e: Event) {
  form.captcha = await performCheck(e);
  await element.value?.validate(async (valid: boolean, _) => {
    if (!valid) return;
    if (valid) {
      loading.value = true;
      axios.post('login/', form)
        .then(function(response) {
          console.log(response);
          error.value = "";
        })
        .catch(function(err) {
          error.value = err.response ? err.response.data['message'] : err.message;
        })
        .finally(function() {
          loading.value = false;
        })
    }
  })
}
</script>

<template>
  <el-container>
    <el-header>
      <RouterLink to="/" class="header">
        <img src="/favicon.ico" alt="Deeptrain">
      </RouterLink>
    </el-header>
    <el-main class="main">
      <h1>Sign in to Deeptrain</h1>
      <el-card shadow="hover" v-loading="loading">
        <el-alert v-if="error" style="transform: translateY(-8px)" :closable="false" :title="error" type="error" show-icon />
        <el-form ref="element" :model="form" :rules="rules" :label-position="'top'">
          <el-form-item label="Username" prop="username">
            <el-input v-model="form.username" type="text" minlength="3" maxlength="14" />
          </el-form-item>
          <el-form-item label="Password" prop="password">
            <el-input v-model="form.password" type="password" show-password minlength="6" maxlength="26" />
          </el-form-item><br>
          <el-button class="validate-button" @click="submit">Sign in</el-button>
        </el-form>
        <el-divider />
        <div class="oauth">
          <o-link uri="https://github.com/"><github /></o-link>
          <o-link uri="https://gitee.com/"><gitee /></o-link>
        </div>
      </el-card>
      <el-card shadow="never" class="help">
        <div>Do not have an account? <RouterLink to="/register">Create one</RouterLink>.</div>
        <div>Forgot password? <RouterLink to="/forgot">Reset password</RouterLink>.</div>
      </el-card>
    </el-main>
  </el-container>
</template>

<style scoped>
@import "@/assets/sytle/user.css";
</style>
